package com.ray;

import com.ray.jobs.MyJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {


    //定义创建 JobDetail实例
    @Bean
    public JobDetail getJobDetail(){
        return JobBuilder.newJob(MyJob.class).storeDurably().build();
    }

    //创建触发器
    @Bean
    public Trigger trigger01(){
        //
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //2秒一次
                .withIntervalInSeconds(2)
                //永久执行
                .repeatForever();
        Trigger trigger = TriggerBuilder.newTrigger()
                //触发器及分组
                .withIdentity("trigger01","group01")
                //规则 Schedule
                .withSchedule(simpleScheduleBuilder)
                .forJob(getJobDetail())
                .build();
        return trigger;
    }


    /**
     * 复杂触发器
     * 表达式
     */
    @Bean
    public Trigger trigger02(){
        //表达式，五秒一次
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ? *");
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                //取个名字和组
                .withIdentity("trigger022", "group022")
                //规则
                .withSchedule(scheduleBuilder)
                //执行哪个任务
                .forJob(getJobDetail())
                .build();
        return trigger;
    }


}
